package joney.main.views;

import java.text.DateFormat;
import java.util.Date;
import java.util.Vector;

import joney.main.analysis.ExpenseUtils;
import joney.main.utils.JoneyUtil;
import joney.main.views.table.ExpenseLabelProvider;
import joney.model.Expense;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.part.ViewPart;

public class ExpenseTypeList extends ViewPart{

	public static final String ID ="joney.expensereport";
	public static int viewsOpen =0;
	
	private Table table;
	private double totalAmount = 0;
	
	public void createPartControl(Composite parent) {
		
		table = createTable(parent);
	}

	public void loadTableNames(String name)
	{
		loadTable(name, null, true);
	}
	
	public void loadTableType(String type)
	{
		loadTable(type, null, false);
	}
	
	public void loadTable(String type, String account, boolean name)
	{
		Vector<Expense> es;
		
		if(name){
			if(account == null)
				es = ExpenseUtils.getAllExpenseOfNameT(type);
			else
				es = ExpenseUtils.getAllExpenseOfNameT(type, account);
		}
		else{
		
		if(account == null)
			es = ExpenseUtils.getAllExpenseOfTypeT(type);
		else
			es = ExpenseUtils.getAllExpenseOfTypeT(type, account);
		}
		
		
		for(Expense e: es)
			addTableValue(e);
		
		
		//add total line to table
		String vals[] = {
				"Total", "","", "", String.valueOf(totalAmount)
		};
		
		TableItem ti = new TableItem(table, SWT.None);
		ti.setBackground(new Color(JoneyUtil.getDisplay(),238,233,233));
		ti.setText(vals);
	}
	
	
	/**
	 * 
	 * Add a value to the table
	 * @param e
	 */
	private void addTableValue(Expense e)
	{
		String vals[] = {
				DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(e.getDate())).toString(),
				e.getName() , e.getType(), e.getAssociatedAccount().getName(), String.valueOf(e.getAmount())
		};
		
		TableItem ti = new TableItem(table, SWT.None);
		ti.setText(vals);
		totalAmount+=e.getAmount();
		
	}
	
	
	private Table createTable(Composite parent)
	{
		int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL
				| SWT.FULL_SELECTION | SWT.HIDE_SELECTION;

		// final int NUMBER_COLUMNS = 4;

		Table table = new Table(parent, style);

		GridData gridData = new GridData(GridData.FILL_BOTH);
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalSpan = 3;
		table.setLayoutData(gridData);

		table.setLinesVisible(true);
		table.setHeaderVisible(true);

		int colNum = 0;

		TableColumn column = new TableColumn(table, SWT.LEFT, colNum++);
		column.setText(ExpenseLabelProvider.DATE);
		column.setWidth(180);
		// Add listener to column so tasks are sorted by description when
		// clicked
		column.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e) {
				// tableViewer.setSorter(new ExampleTaskSorter(
				// ExampleTaskSorter.DESCRIPTION));
			}
		});

		column = new TableColumn(table, SWT.LEFT, colNum++);
		column.setText(ExpenseLabelProvider.NAME);
		column.setWidth(120);
		// Add listener to column so tasks are sorted by description when
		// clicked
		column.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e) {
				// tableViewer.setSorter(new ExampleTaskSorter(
				// ExampleTaskSorter.DESCRIPTION));
			}
		});

		column = new TableColumn(table, SWT.LEFT, colNum++);
		column.setText(ExpenseLabelProvider.TYPE);
		column.setWidth(120);
		// Add listener to column so tasks are sorted by owner when clicked
		column.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e) {
				// tableViewer.setSorter(new ExampleTaskSorter(
				// ExampleTaskSorter.OWNER));
			}
		});
		
		column = new TableColumn(table, SWT.LEFT, colNum++);
		column.setText("Account");
		column.setWidth(120);
		// Add listener to column so tasks are sorted by owner when clicked
		column.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e) {
				// tableViewer.setSorter(new ExampleTaskSorter(
				// ExampleTaskSorter.OWNER));
			}
		});

		column = new TableColumn(table, SWT.CENTER, colNum++);
		column.setText("Amount");
		column.setWidth(120);
		// Add listener to column so tasks are sorted by percent when clicked
		column.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e) {
				// tableViewer.setSorter(new ExampleTaskSorter(
				// ExampleTaskSorter.PERCENT_COMPLETE));
			}
		});

		return table;
	}
	
	public void setFocus() {
	}

}
